package com.kitty.game.difushidao.controller;


import com.kitty.common.cache.RedisService;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.difushidao.RespDiFuShidaoPiPei;
import com.kitty.game.difushidao.service.DiFuShiDaoPKService;
import com.kitty.game.difushidao.service.DiFuShiDaoService;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.role.model.Role;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.team.model.Member;
import com.kitty.game.team.model.Team;
import com.kitty.game.team.service.TeamService;
import com.kitty.game.utils.Const;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.cache.SessionUtils;

import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import static java.lang.Thread.sleep;

@Controller
@Slf4j
public class DiFuShiDaoController {
    @Autowired
    protected TeamService teamService;

    @Autowired
    DiFuShiDaoPKService diFuShiDaoPKService;
    @Autowired
    DiFuShiDaoService diFuShiDaoService;
    @Autowired
    RedisService redisService;
    /**
     * 地府世道队伍匹配队列
     */
    public static Map<String, Set<Team>> piPeiQueByLevel = new ConcurrentHashMap<>();

    @RequestMapping
    public void DiFuShiDaoPiPei(IoSession session, RespDiFuShidaoPiPei respDiFuShidaoPiPei) {
        Role role = SessionUtils.getRoleBySession(session);
        int statu = respDiFuShidaoPiPei.getStatus();
        String difushidao_status = DataCache.REDIS_DATA.get("difushidao_status").toString();
        //竞技大赛是否开始
        if(!"2".equals(difushidao_status)){
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("别着急，地府竞技场战斗还未开始！！！"));
            return;
        }
        //是否组队
        if(!teamService.isInTeam(role)){
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("请组队！！！"));
            return;
        }
        //是否是队长， 不是队长不可以点匹配。
        if(!teamService.isTeamLeader(role)){
            MessagePusher.pushMessage(role,new RespNotifyMiscEx(Const.NOT_LEADER));
            return;
        }
        //获取当前人的 阶段
        String levelRange = diFuShiDaoService.getLevelRange(role);
        Set<Team> teamSet = piPeiQueByLevel.get(levelRange);
        if(teamSet ==null){
            teamSet = new HashSet<>();
            piPeiQueByLevel.put(levelRange,teamSet);
        }
        Team team = teamService.getTeam(role.getRoleId());
        if(team != null){
            if(statu == 1){
                teamSet.add(team);
            }else if(statu == 0){
                teamSet.remove(team);
            }
            for (Member member : team.getList()) {
                Role memberRole = SpringUtils.getRoleService().getOnlinePlayer(member.getRoleId());
                diFuShiDaoPKService.sendRespShidaoScore(memberRole,statu);
            }
        }else{
            MessagePusher.pushMessage(role, new RespMsg("请创建队伍！"));
        }
    }
}
